﻿using System.Linq;
using System.Web.Mvc;
using Century.Web.Security.Attributes
using Century.Model;
using GKFX.Common.Business.BOL.Common;
using GKFX.Common.UI.MVC;
using GKFX.Models.ViewModels;
using GKFX.Business.Repository;
using System.Collections.Generic;
using Century.EnterpriseLibrary.Configuration;
using GKFX.Globalization;
using GKFX.Common.Enumerations;
using System;

namespace Century.Web.MVC.AdminArea
{
    [Authorization(Group = "", Name = "SiteManager")]
    public class SiteManagementController : CenturyAdminBaseController
    {
        public ActionResult Index()
        {
            return View();
        }

        #region Social Links

        public ActionResult SocialNetworks()
        {
            return View();
        }
        
        [HttpPost]
        public void SaveSocialNetworks(FormCollection elements)
        {
            Dictionary<string, string> links = new Dictionary<string,string>();
            
            links.Add(SiteConfigKey.FacebookLink.ToString(), elements[SiteConfigKey.FacebookLink.ToString()]);
            links.Add(SiteConfigKey.TwitterLink.ToString(), elements[SiteConfigKey.TwitterLink.ToString()]);
            links.Add(SiteConfigKey.YoutubeLink.ToString(), elements[SiteConfigKey.YoutubeLink.ToString()]);
            links.Add(SiteConfigKey.LinkedInLink.ToString(), elements[SiteConfigKey.LinkedInLink.ToString()]);
            links.Add(SiteConfigKey.RssFeedLink.ToString(), elements[SiteConfigKey.RssFeedLink.ToString()]);
            links.Add(SiteConfigKey.VimeoLink.ToString(), elements[SiteConfigKey.VimeoLink.ToString()]);
            links.Add(SiteConfigKey.GooglePlusLink.ToString(), elements[SiteConfigKey.GooglePlusLink.ToString()]);
            
            SiteConfigReader.Instance.SaveSocialNetworkLink(links);
            SiteConfigReader.Instance.Reload();
        }
        
        #endregion

        #region Site Configs

        public ActionResult SiteConfigs()
        {
            using (CountrySiteEmailBSO csBSO = new CountrySiteEmailBSO())
            {
                foreach (GKFX.Common.Enumerations.EmailConfigType type in Enum.GetValues(typeof(GKFX.Common.Enumerations.EmailConfigType)))
                {
                    TempData[type.ToString()] = csBSO.GetEmailConfig(type, PersistData.CurrentSiteId);
                }
            }
            return View();
        }

        [HttpPost]
        public ActionResult SiteConfigs(FormCollection elements)
        {
                // contact phone
                string contactPhone = elements[SiteConfigKey.ContactPhone.ToString()];
                SiteConfigReader.Instance.SaveConfig(SiteConfigKey.ContactPhone, contactPhone);
                SiteConfigReader.Instance.Reload();

                // save country site emails
                using (CountrySiteEmailBSO csBSO = new CountrySiteEmailBSO())
                {
                    foreach (GKFX.Common.Enumerations.EmailConfigType type in Enum.GetValues(typeof(GKFX.Common.Enumerations.EmailConfigType)))
                    {
                        csBSO.SaveEmailConfig(type, elements[type.ToString()], PersistData.CurrentSiteId);
                    }
                }
            
            return View();
        }
        
        #endregion

        protected SitePage GetSitePage(int? pageID)
        {
            using (SitePageBSO sp = new SitePageBSO())
            {
                return sp.GetById(pageID.HasValue ? pageID.Value : 0);
            }
        }

        #region Page Context Menu Actions

        public ActionResult AddNewSubPage(int? id)
        {
            return View(GetSitePage(id));
        }

        public ActionResult CopyPageAs(int? id)
        {
            return View(GetSitePage(id));
        }

        public ActionResult DisableOrEnablePage(int? id)
        {
            return View(GetSitePage(id));
        }

        #endregion

        #region Site Context Menu Actions

        public ActionResult AddNewPage(int? id)
        {
            return View();
        }

        public ActionResult CopySiteAs(int? id)
        {
            using (SitePageBSO sp = new SitePageBSO())
            {
                PageDetail pageDetail = new PageDetail();
                pageDetail.Page = sp.GetAll().Where(p => p.Id == (id.HasValue ? id.Value : 0)).FirstOrDefault();
                return View(pageDetail);
            }
        }

        public ActionResult DisableOrEnableSite(int? id)
        {
            using (SitePageBSO sp = new SitePageBSO())
            {
                PageDetail pageDetail = new PageDetail();
                pageDetail.Page = sp.GetAll().Where(p => p.Id == (id.HasValue ? id.Value : 0)).FirstOrDefault();
                return View(pageDetail);
            }
        }


        #endregion
    }
}
